(function() {
    'use strict';

    var global = window.tinymce;

    function getSelectedNode(editor) {
        var node = editor.selection.getNode();
        return node.nodeType === 3 ? node.parentNode : node; // 如果是文本节点,获取其父节点
    }

    // 获取节点的所有样式
    function getNodeStyles(editor, node) {
        var styles = {};
        var dom = editor.dom;
        
        // 获取计算后的样式
        var computed = window.getComputedStyle(node);
        
        // 复制关键样式属性
        var styleProps = [
            'fontFamily',
            'fontSize',
            'fontWeight',
            'fontStyle',
            'color',
            'backgroundColor',
            'textDecoration',
            'textAlign',
            'lineHeight',
            'letterSpacing',
            'textIndent',
            'verticalAlign',
            'textTransform',
            'borderStyle',
            'borderWidth',
            'borderColor',
            'borderCollapse',
            'borderSpacing'
        ];

        styleProps.forEach(function(prop) {
            var value = computed[prop];
            if(value && value !== 'none' && value !== 'normal') {
                styles[prop] = value;
            }
        });

        // 获取应用的类
        var classes = dom.getAttrib(node, 'class');
        if(classes) {
            styles.classes = classes;
        }

        return styles;
    }

    // 应用样式到选区
    function applyStylesToSelection(editor, styles) {
        var dom = editor.dom;
        var selection = editor.selection;
        var selectedContent = selection.getContent(); // 获取选中的内容

        // 如果没有选区,返回
        if(selection.isCollapsed() || !selectedContent) {
            return;
        }

        var bookmark = selection.getBookmark();

        // 创建一个临时容器包装选区内容
        var wrapper = dom.create('span', {}, selectedContent);

        // 应用样式
        if(styles.classes) {
            dom.setAttrib(wrapper, 'class', styles.classes);
        }

        // 应用内联样式
        for(var prop in styles) {
            if(prop !== 'classes') {
                dom.setStyle(wrapper, prop, styles[prop]);
            }
        }

        // 替换选中内容
        selection.setContent(wrapper.outerHTML);

        // 恢复选区
        selection.moveToBookmark(bookmark);

        // 触发change事件
        editor.nodeChanged();
    }

    function Plugin(editor) {
        var formatPainterState = false;
        var storedStyles = null;

        // 添加格式刷按钮
        editor.ui.registry.addToggleButton('formatpainter', {
            icon: 'format-painter',
            tooltip: '格式刷',
            onAction: function() {
                if(!formatPainterState) {
                    // 获取样式
                    var node = getSelectedNode(editor);
                    storedStyles = getNodeStyles(editor, node);
                    formatPainterState = true;
                    
                    // 添加格式刷光标样式
                    editor.dom.addClass(editor.getBody(), 'tox-cursor-format-painter');
                } else {
                    // 关闭格式刷
                    formatPainterState = false;
                    storedStyles = null;
                    editor.dom.removeClass(editor.getBody(), 'tox-cursor-format-painter');
                }
            },
            onSetup: function(api) {
                editor.getBody().addEventListener('mouseup', function () {
                    if(formatPainterState && storedStyles) {
                        applyStylesToSelection(editor, storedStyles);
                        
                        // 应用后关闭格式刷
                        formatPainterState = false;
                        storedStyles = null;
                        editor.dom.removeClass(editor.getBody(), 'tox-cursor-format-painter');
                        api.setActive(false);
                    }
                })

                // ESC键关闭格式刷
                editor.on('keydown', function(e) {
                    if(e.keyCode === 27 && formatPainterState) {
                        formatPainterState = false;
                        storedStyles = null;
                        editor.dom.removeClass(editor.getBody(), 'tox-cursor-format-painter');
                        api.setActive(false);
                    }
                });

                return function() {};
            }
        });
    }

    global.PluginManager.add('formatpainter', function(editor) {
        Plugin(editor);
    });
})();
